/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Main;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author hoabui
 */
public class frmQLDeThi extends javax.swing.JFrame {

    /**
     * Creates new form frmQLDeThe
     */
    public frmQLDeThi() {
        initComponents();
        layDSDeThi();
    }
    //Danh sach de thi
    List<DeThi> dsDeThi = new ArrayList<DeThi>();

    //Lay danh sach de thi
    private void layDSDeThi() {
        //Lay danh sach de thi theo username
        dsDeThi = DeThi.layDanhSachDeThiTheoNguoiDung(Main.nguoiDungDangNhap.getUserName());
        //Tao model cho jtable
        DefaultTableModel model = new DefaultTableModel();
        //Tao cot cho model
        model.addColumn("Mã đề");
        model.addColumn("Tiêu đề");

        //Them dong cho model
        for (int i = 0; i < dsDeThi.size(); i++) {
            DeThi dt = dsDeThi.get(i);
            Vector v = new Vector();
            v.add(dt.getMaDe());
            v.add(dt.getTieuDe());
            model.addRow(v);
        }

        //Set model cho jtable
        tblDSDeThi.setModel(model);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        tfTieuDe = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblDSDeThi = new javax.swing.JTable();
        btnThem = new javax.swing.JButton();
        btnXoa = new javax.swing.JButton();
        btnCapNhat = new javax.swing.JButton();
        btnThoat = new javax.swing.JButton();
        btnThemBaiThi = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("QUẢN LÝ ĐỀ THI");

        jLabel2.setText("Tiêu đề");

        tblDSDeThi.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tblDSDeThi.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblDSDeThiMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblDSDeThi);

        btnThem.setText("Thêm");
        btnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemActionPerformed(evt);
            }
        });

        btnXoa.setText("Xóa");
        btnXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXoaActionPerformed(evt);
            }
        });

        btnCapNhat.setText("Cập nhật");
        btnCapNhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCapNhatActionPerformed(evt);
            }
        });

        btnThoat.setText("Thoát");
        btnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoatActionPerformed(evt);
            }
        });

        btnThemBaiThi.setText("Thêm bài thi");
        btnThemBaiThi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemBaiThiActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(137, 137, 137))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 361, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap())))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnThem)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnXoa)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnCapNhat)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnThoat)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(tfTieuDe))
                        .addContainerGap())))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnThemBaiThi)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tfTieuDe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnThem)
                    .addComponent(btnXoa)
                    .addComponent(btnCapNhat)
                    .addComponent(btnThoat))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 290, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnThemBaiThi)
                .addGap(4, 4, 4))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    //Su kien nut them
    private void btnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemActionPerformed
        // TODO add your handling code here:
        //Kiem tra neu co nhap tieu de thi se them
        if (tfTieuDe.getText().length() > 0) {
            DeThi dt = new DeThi();
            dt.setTieuDe(tfTieuDe.getText());
            dt.setUserName(Main.nguoiDungDangNhap.getUserName());
            if (dt.ThemDeThi()) {
                JOptionPane.showMessageDialog(null, "Thêm đề thi thành công", "Thông báo", JOptionPane.PLAIN_MESSAGE);
                layDSDeThi();
            } else {
                JOptionPane.showMessageDialog(null, "Thêm đề thi thất bại", "Thông báo", JOptionPane.PLAIN_MESSAGE);
            }

        } //Nguoc lai bao loi
        else {
            JOptionPane.showMessageDialog(null, "Vui lòng nhập tiêu đề", "Thông báo", JOptionPane.PLAIN_MESSAGE);
        }
    }//GEN-LAST:event_btnThemActionPerformed

    //Su kien chon vao 1 dong cua jtbale
    private void tblDSDeThiMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblDSDeThiMouseClicked
        // TODO add your handling code here:
        //Lay vi tri dong dang chon
        int index = tblDSDeThi.getSelectedRow();
        //Neu vi tri >= 0 thi hien thi thong tin len form
        if (index >= 0) {
            DeThi dt = dsDeThi.get(index);
            tfTieuDe.setText(dt.getTieuDe());
        }
    }//GEN-LAST:event_tblDSDeThiMouseClicked

    //Su kien bam nut xoa
    private void btnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXoaActionPerformed
        // TODO add your handling code here:
        //Lay vi tri dong duoc tron tren jtable
        int index = tblDSDeThi.getSelectedRow();
        //Neu vi tri >=0 thi tien hanh xoa de thi
        if (index >= 0) {
            DeThi dt = dsDeThi.get(index);
            if (DeThi.XoaDeThi(dt.getMaDe())) {
                JOptionPane.showMessageDialog(null, "Xóa đề thi thành công", "Thông báo", JOptionPane.PLAIN_MESSAGE);
                layDSDeThi();
            } else {
                JOptionPane.showMessageDialog(null, "Xóa đề thi thất bại", "Thông báo", JOptionPane.PLAIN_MESSAGE);
            }

        } else {
            JOptionPane.showMessageDialog(null, "Vui lòng chọn bài thi để xóa", "Thông báo", JOptionPane.PLAIN_MESSAGE);
        }
    }//GEN-LAST:event_btnXoaActionPerformed

    //Su kien nut cap nhat 
    private void btnCapNhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCapNhatActionPerformed
        // TODO add your handling code here:
        //Lay vi tri dong duoc tron tren jtable
        int index = tblDSDeThi.getSelectedRow();
        //Neu vi tri >=0 thi tien hanh cap nhat de thi
        if (index >= 0) {
            DeThi dt = dsDeThi.get(index);
            if (tfTieuDe.getText().length() > 0) {

                dt.setTieuDe(tfTieuDe.getText());
                if (dt.CapNhatDeThi()) {
                    JOptionPane.showMessageDialog(null, "Cập nhật đề thi thành công", "Thông báo", JOptionPane.PLAIN_MESSAGE);
                    layDSDeThi();
                } else {
                    JOptionPane.showMessageDialog(null, "Cập nhật đề thi thất bại", "Thông báo", JOptionPane.PLAIN_MESSAGE);
                }

            } else {
                JOptionPane.showMessageDialog(null, "Vui lòng nhập tiêu đề", "Thông báo", JOptionPane.PLAIN_MESSAGE);
            }

        } else {
            JOptionPane.showMessageDialog(null, "Vui lòng chọn bài thi để cập nhật", "Thông báo", JOptionPane.PLAIN_MESSAGE);
        }


    }//GEN-LAST:event_btnCapNhatActionPerformed

    //Su kien nut thoat
    private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoatActionPerformed
        // TODO add your handling code here:
        frmLogin frm = new frmLogin();
        frm.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_btnThoatActionPerformed

    //Su kien nut them bai thi
    private void btnThemBaiThiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemBaiThiActionPerformed
        // TODO add your handling code here:
        //Lay dong da chon tren table
        int index = tblDSDeThi.getSelectedRow();
        //Neu vi tri >= 0 thi hien thi danh sach bai thi tuong ung voi de thi da chon
        if (index >= 0) {
            DeThi dt = dsDeThi.get(index);
            frmQLBaiThi frm = new frmQLBaiThi();
            frm.setDeThi(dt);
            frm.setVisible(true);

        } else {
            JOptionPane.showMessageDialog(null, "Vui lòng chọn đề thi", "Thông báo", JOptionPane.PLAIN_MESSAGE);
        }

    }//GEN-LAST:event_btnThemBaiThiActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCapNhat;
    private javax.swing.JButton btnThem;
    private javax.swing.JButton btnThemBaiThi;
    private javax.swing.JButton btnThoat;
    private javax.swing.JButton btnXoa;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblDSDeThi;
    private javax.swing.JTextField tfTieuDe;
    // End of variables declaration//GEN-END:variables
}
